import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";

const router = createRouter({
  history: createWebHistory(), // history
  // history: createWebHashHistory(), // hash
  routes: [
    {
      path: "/home",
      component: () => import("../views/home/index.vue"),
      name: "Home",
      meta: {
        title: "首页",
      },
      children: [
        {
          // path: "/home/message",
          path: "message", // 不以 / 开头，会自动补全父路由地址
          component: () => import("../views/message/index.vue"),
          name: "Message",
        },
      ],
    },
    {
      path: "/about",
      component: () => import("../views/about/index.vue"),
      name: "About",
      meta: {
        title: "关于",
      },
    },
    {
      path: "/detail/:id",
      component: () => import("../views/detail/index.vue"),
      name: "Detail",
    },
    {
      // https://router.vuejs.org/zh/guide/essentials/dynamic-matching.html#%E6%8D%95%E8%8E%B7%E6%89%80%E6%9C%89%E8%B7%AF%E7%94%B1%E6%88%96-404-not-found-%E8%B7%AF%E7%94%B1
      // 匹配任意路由
      path: "/:pathMatch(.*)*",
      redirect: "/home",
    },
  ],
});

router.beforeEach((to, from, next) => {
  // to 要去的路由对象
  // from 从哪来的路由对象
  next();
});

export default router;
